//
//  UFOLQCNewViewController.m
//  unrivalledForUniverse
//
//  Created by dllo on 1/11/16.
//  Copyright © 2016 dllo. All rights reserved.
//

#import "UFOLQCNewViewController.h"
#import "UFOLQCNewCollertionViewCell.h"
#import "UFOLQCMatchCollectionViewCell.h"
#import "UFOLQCPlayCollectionViewCell.h"
#import "UFOLQCActivityCollectionViewCell.h"

#import "UFOLQCNewModel.h"
#import "UFOLQCWebViewController.h"
#import "LQCAFNetTool.h"
@interface UFOLQCNewViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, setupWebView, setupWebViewMatch, setupWebViewPlay, setupWebViewActivity>

@property (nonatomic, retain) UICollectionView *mainCollectionV;
@property (nonatomic, retain) UIButton *button;

@property (nonatomic, retain) NSMutableArray *firstArr;
@property (nonatomic, retain) NSMutableArray *matchArr;
@property (nonatomic, retain) NSMutableArray *playArr;
@property (nonatomic, retain) NSMutableArray *activityArr;

@property (nonatomic, copy) NSString *firstStr;
@property (nonatomic, copy) NSString *matchStr;
@property (nonatomic, copy) NSString *playStr;
@property (nonatomic, copy) NSString *activityStr;



@end

@implementation UFOLQCNewViewController


- (void)dealloc{
    self.mainCollectionV.delegate = nil;
    self.mainCollectionV.dataSource = nil;
    [_mainCollectionV release];
    [_button release];
    [_firstArr release];
    [_matchArr release];
    [_playArr release];
    [_activityArr release];
    
    [_firstStr release];
    [_matchStr release];
    [_playStr release];
    [_activityStr release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatButton];
    [self createCollectionV];
    
    [self createRequesNewData];
    [self createRequesMatchData];
    [self createRequesPlayData];
    [self createRequesActivityData];
}

- (void)creatButton{
    NSArray *informationArr = @[@"首页",@"赛事",@"娱乐", @"活动"];
    for (NSInteger i = 0; i < 4; i++) {
        self.button = [UIButton buttonWithType:UIButtonTypeCustom];
        self.button.frame = CGRectMake(i * self.view.frame.size.width / 4, 4, self.view.frame.size.width / 4, 40);
        
        self.button.tag = i + 1;
        
        [self.button setTitle:informationArr[i] forState:UIControlStateNormal];
        
        [self.button setTitleColor:[UIColor colorWithRed:0.572 green:0.181 blue:1.000 alpha:1.000] forState:UIControlStateNormal];
        [self.button setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];
        
        self.button.backgroundColor = [UIColor redColor];
        
        [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.navigationController.navigationBar addSubview:self.button];
    }
}

// button 点击
- (void)buttonAction:(UIButton *)button{
    
    self.mainCollectionV.contentOffset = CGPointMake(SCREENWIDTH * (button.tag - 1), - NCHEIGHT);
    for (NSInteger i = 1; i < 5; i++) {
        UIButton *tempB = [self.navigationController.navigationBar viewWithTag:i];
        if (tempB.selected == YES) {
            tempB.selected = NO;
        }
        if (i == button.tag) {
            button.selected = YES;
        }
    }
    
}

// collectionV   关联 导航栏
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    for (NSInteger i = 1; i < 5; i++) {
        UIButton *tempB = [self.navigationController.navigationBar viewWithTag:i];
        if (tempB.selected == YES) {
            tempB.selected = NO;
        }
        if (i == self.mainCollectionV.contentOffset.x / SCREENWIDTH + 1) {
            tempB.selected = YES;
        }
    }
}

//  collection View
- (void)createCollectionV{
    UICollectionViewFlowLayout *flow = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flow.minimumLineSpacing = 0;
    flow.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 64 - 49);
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    self.mainCollectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flow];
    self.mainCollectionV.pagingEnabled = YES;
    self.mainCollectionV.delegate = self;
    self.mainCollectionV.dataSource = self;
    self.mainCollectionV.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.mainCollectionV];
    
    [_mainCollectionV release];
    
    [self.mainCollectionV registerClass:[UFOLQCNewCollertionViewCell class] forCellWithReuseIdentifier:@"cellNew"];
    [self.mainCollectionV registerClass:[UFOLQCMatchCollectionViewCell class] forCellWithReuseIdentifier:@"cellMatch"];
    [self.mainCollectionV registerClass:[UFOLQCPlayCollectionViewCell class] forCellWithReuseIdentifier:@"cellPlay"];
    [self.mainCollectionV registerClass:[UFOLQCActivityCollectionViewCell class] forCellWithReuseIdentifier:@"cellActivity"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (0 == indexPath.item) {
        UFOLQCNewCollertionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellNew" forIndexPath:indexPath];
        cell.delegate = self;
        cell.dataArr = self.firstArr;
        cell.nextStr = self.firstStr;
        return cell;
    }
    else if (1 == indexPath.item){
        
        UFOLQCMatchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellMatch" forIndexPath:indexPath];
        cell.delegate = self;
        cell.dataArr = self.matchArr;
        return cell;
    }
    else if (2 == indexPath.item){
        UFOLQCPlayCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellPlay" forIndexPath:indexPath];
        cell.delegate = self;
        cell.dataArr = self.playArr;
        return cell;
 
    }
    else if (3 == indexPath.item){
        
        UFOLQCActivityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellActivity" forIndexPath:indexPath];
        cell.delegate = self;
        cell.dataArr = self.activityArr;
        return cell;
    }
    return nil;
}

// 第一页
- (void)createRequesNewData{
    [LQCAFNetTool getNetWithURL:@"http://qt.qq.com/static/pages/news/phone/c12_list_1.shtml" body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *dataArr = [responseObject objectForKey:@"list"];
        self.firstArr = [UFOLQCNewModel baseModelWithArr:dataArr];
        self.firstStr = [responseObject objectForKey:@"next"];
        
        [self.mainCollectionV reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
 
    }];
}
// 第二页
- (void)createRequesMatchData{
    [LQCAFNetTool getNetWithURL:@"http://qt.qq.com/static/pages/news/phone/c73_list_1.shtml" body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *dataArr = [responseObject objectForKey:@"list"];
        self.matchArr = [UFOLQCNewModel baseModelWithArr:dataArr];
        self.matchStr = [responseObject objectForKey:@"next"];
        [self.mainCollectionV reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}
// 第三页
- (void)createRequesPlayData{
    [LQCAFNetTool getNetWithURL:@"http://qt.qq.com/static/pages/news/phone/c18_list_1.shtml" body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *dataArr = [responseObject objectForKey:@"list"];
        self.playArr = [UFOLQCNewModel baseModelWithArr:dataArr];
        self.playStr = [responseObject objectForKey:@"next"];
        [self.mainCollectionV reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
// 第四页
- (void)createRequesActivityData{
    [LQCAFNetTool getNetWithURL:@"http://qt.qq.com/static/pages/news/phone/c23_list_1.shtml" body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *dataArr = [responseObject objectForKey:@"list"];
        self.activityArr = [UFOLQCNewModel baseModelWithArr:dataArr];
        self.activityStr = [responseObject objectForKey:@"next"];
        [self.mainCollectionV reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
}

// 第一页 加载
- (void)createRequesNewAddDate{
    [LQCAFNetTool getNetWithURL:[NSString stringWithFormat:@"http://qt.qq.com/static/pages/news/phone/%@", self.firstStr]body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *dataArr = [responseObject objectForKey:@"list"];
        
        [self.firstArr addObjectsFromArray:[UFOLQCNewModel baseModelWithArr:dataArr]];
        
        self.firstStr = [responseObject objectForKey:@"next"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"firstC" object:nil];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
// 第二页 加载
- (void)createRequesMatchAddDate{
    [LQCAFNetTool getNetWithURL:[NSString stringWithFormat:@"http://qt.qq.com/static/pages/news/phone/%@", self.matchStr]body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *dataArr = [responseObject objectForKey:@"list"];
        [self.matchArr addObjectsFromArray:[UFOLQCNewModel baseModelWithArr:dataArr]];
        self.matchStr = [responseObject objectForKey:@"next"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"matchC" object:nil];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
// 第三页 加载
- (void)createRequesPlayAddDate{
    [LQCAFNetTool getNetWithURL:[NSString stringWithFormat:@"http://qt.qq.com/static/pages/news/phone/%@", self.playStr]body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *dataArr = [responseObject objectForKey:@"list"];
        [self.playArr addObjectsFromArray:[UFOLQCNewModel baseModelWithArr:dataArr]];
        self.playStr = [responseObject objectForKey:@"next"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"playC" object:nil];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
// 第四页 加载
- (void)createRequesActivityAddDate{
    [LQCAFNetTool getNetWithURL:[NSString stringWithFormat:@"http://qt.qq.com/static/pages/news/phone/%@", self.activityStr]body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableArray *dataArr = [responseObject objectForKey:@"list"];
        [self.activityArr addObjectsFromArray:[UFOLQCNewModel baseModelWithArr:dataArr]];
        self.activityStr = [responseObject objectForKey:@"next"];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"activityC" object:nil];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

//协议方法  跳转页面
- (void)setupWebViewWithUrl:(NSString *)url{
    UFOLQCWebViewController *webVC = [[[UFOLQCWebViewController alloc] init] autorelease];
    webVC.url = url;
    [self presentViewController:webVC animated:YES completion:^{
      
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
